/**
 * @file ext-link.js
 *
 *
 * @copyright 2010 CloudCanvas, Inc. All rights reserved
 * @copyright 2021 Optimistik SAS, Inc. All rights reserved
 * @license MIT
 *
 */
 const name = 'text'
 export default {
     name,
   init: async function () {
     const svgEditor = this
     const {svgCanvas} = svgEditor
     const {ChangeElementCommand} = svgCanvas.history
     const addToHistory = (cmd) => {
       svgCanvas.undoMgr.addCommandToHistory(cmd)
     }
     const {$id, $click, $qq} = svgCanvas
     const setAttrs = () => {

     }
     return {
       name: 'text',
       callback() {

       },
       updateText(val) {
         // <text class="fill_solidColor" doc-type="text" fill="#000000"
         //  font-family="Serif" font-size="24" font-style="italic"
         //  font-weight="bold" id="svg_2" stroke="gray" stroke-dasharray="5,5"
         //   stroke-width="0" text-anchor="middle" text-decoration="line-through  underline"
         //   x="290" xml:space="preserve" y="120">dsfs</text>
         if (val.mode == "text") {
           const elem = svgCanvas.getSelectedElements()[0]
           var opts = val.data
           if (elem) {
             console.log(elem)
             elem.setAttribute("state", btoa(unescape(encodeURIComponent(JSON.stringify(opts)))))
             if (opts.typeVal.indexOf("bold") > -1) {
               svgCanvas.setBold(true)
             } else {
               svgCanvas.setBold(false)
             }
             if (opts.typeVal.indexOf("italic") > -1) {
               svgCanvas.setItalic(true)
             } else {
               svgCanvas.setItalic(false)
             }
             if (opts.typeVal.indexOf("line-through") > -1) {
               svgCanvas.removeTextDecoration("line-through")
               svgCanvas.addTextDecoration("line-through")
             } else {
               svgCanvas.removeTextDecoration("line-through")
             }
             if (opts.typeVal.indexOf("underline") > -1) {
               svgCanvas.removeTextDecoration("underline")
               svgCanvas.addTextDecoration("underline")
             } else {
               svgCanvas.removeTextDecoration("underline")
             }
             if (opts.fontdir) {
               elem.setAttribute("writing-mode", opts.fontdir)
             }
             if (opts.fontfirst) {
               svgCanvas.setFontFamily(opts.fontfirst)
             }
             if (opts.fontnumber) {
               svgCanvas.setFontSize(opts.fontnumber)
             }
             if (opts.desc) {
               if (opts.fontdir == "lr" && (opts.desc.indexOf("\n") > -1)) {
                 var texts = opts.desc.split("\n")
                 elem.textContent = ""
                 for (var i = 0; i < texts.length; i++) {
                   var tspan = document.createElementNS('http://www.w3.org/2000/svg', 'tspan')
                   tspan.setAttribute("x", elem.getAttribute("x"))
                   tspan.setAttribute("y", parseInt(elem.getAttribute("y")) + parseInt(elem.getBBox().height))
                   tspan.append(texts[i])
                   elem.append(tspan)
                 }
               } else {
                 elem.textContent = opts.desc
                 svgCanvas.recalculateDimensions(elem)
                 svgCanvas.selectorManager.requestSelector(elem).resize()
               }
             }
             svgEditor.layersPanel.populateObjects()
            //  addToHistory(new ChangeElementCommand(elem,{state:btoa(unescape(encodeURIComponent(JSON.stringify(opts))))}))
           }
         }
       }
     }

   }
 }
